import '../public/index'
import url from '../data/url'
import request from '../data/request'
import {tableConfig} from '../data/config'
import {getUrlParam} from '../data/util'
import store from 'store'

const base = url.base,
  getDeveloperManageList = request.getDeveloperManageList,
  getBankList = '/clearBankMappingInfo/v1/facade.json',
  removeBankInfo = '/clearBankMappingInfo/v1/modify.json'

let table,
  storeData;

// 初始化表格
table = $('#table').bootstrapTable($.extend({}, tableConfig, {
  url: base + getBankList,
  queryParams: function (params) {
    return $.extend({
      currentPage: params.pageNumber,
      pageSize: params.pageSize,
    }, $('#searchForm').serializeObject())
  },
  columns: [
    {
      field: 'bankCode',
      title: '联行号',
      valign: 'middle'
    }, {
      field: 'name',
      title: '名称',
      valign: 'middle'
    }, {
      field: 'bankBranch',
      title: '行别',
      valign: 'middle'
    }, {
      field: 'clearBankCode',
      title: '清算行号',
      valign: 'middle'
    }, {
      field: 'upClearBankCode',
      title: '上一级清算行的联行号',
      valign: 'middle'
    }, {
      field: '',
      valign: 'middle',
      align: 'center',
      title: '操作',
      formatter: function (index, row) {
        return `<div class="btn-group">
                      <button type="button" data-id="${ row.idStr }" class="btn btn-default btn-modify">修改</button>                  
                      <button type="button" data-id="${ row.idStr }" class="btn btn-default btn-delete">删除</button>                  
                </div>`
      }
    }]
}))

// 点击列表的时候传递数据到全局变量
table.on('click-row.bs.table', (e, data, el) => {
  storeData = data
})

// 搜索
$('#search-btn').on('click', (e) => {
  e.preventDefault();

  table.bootstrapTable('refresh', {
    query: $.extend({
      currentPage: 1,
    }, $('#searchForm').serializeObject())
  })
})

//跳转至修改页
$(document).on('click', '#table .btn-modify', (e) => {
  e.preventDefault()

  console.log('修改')

  store.set('storeData', storeData)
  window.location.href = `./modify.html?id=${ storeData.id }`
})

// 删除
$(document).on('click', '#table .btn-delete', (e) => {
  e.preventDefault()

  let id = $(e.target).attr('data-id'),
    data = {id: id};

  bootbox.confirm({
    title: '提示',
    message: `<div>
                <h3 style="margin:.75em 0">是否删除该清算银行信息？</h3>
              </div>`,
    buttons: {
      cancel: {
        label: '取消'
      },
      confirm: {
        label: '确认'
      }
    },
    callback: (result) => {

      if (!result) {
        return
      }

      let loading_modal = layer.msg('提交中...', {
        icon: 16,
        time: 0
      })

      console.log('data:', data)

      $.ajax({
        type: "POST",
        url: base + '/clearBankMappingInfo/delete.json',
        dataType: "json",
        data: data,
        headers: {
          myToken: Cookies.get('myToken')
        },
      }).done(function (res) {
          console.log(res)

          if (res.code === 0) {

            layer.msg('删除成功', {
              icon: 6
            })

            table.bootstrapTable('refresh')

          } else {
            layer.msg(res.msg, {
              icon: 5
            })
          }

        })
        .fail(function (res) {
          console.log(res)

          layer.msg('删除失败', {
            icon: 5
          })

        })
        .always(function (res) {

          console.log("complete")

          layer.close(loading_modal)
        })
    }
  })
})